
<script lang="ts">
export default {
  name: "list-component"
}
</script>


<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import request from '@/utils/request'
const router = useRouter()
const articles = ref([])
const groups = ref([])

const defaultGroup = ref('1')

onMounted(() => {
  getGroupList().then(() => {
    getArticleList(defaultGroup.value)
  })
})

/**
 * 获取所有分类
 */
const getGroupList = async () => {
  request('/groups').then((data) => {
    groups.value = data
    defaultGroup.value = data[0].id
  })
}

/**
 * 根据分组文章
 * @param groupId
 */
const getArticleList = (groupId: string) => {
  request(`/articles`, { gid: groupId }).then((data) => {
    articles.value = data
    defaultGroup.value = groupId
  })
}
const getArticleInfo = (id: string) => {
  router.push({
    path: '/article',
    query: {
      id
    }
  })
}
</script>

<template>
  <ul class="sub-nav">
    <li
      :class="{ active: defaultGroup === group.id }"
      :key="group.id"
      v-for="group in groups"
      @click="getArticleList(group.id)"
    >
      {{ group.name }}
    </li>
  </ul>

  <div class="list">
    <div class="item" :key="index" v-for="(art, index) in articles">
      <div class="avatar">
        <!--        <img class="image"  alt="" />-->
      </div>
      <div class="title" @click="getArticleInfo(art.id)">{{ art.title }}</div>
    </div>
  </div>
</template>

<style scoped>
.sub-nav {
  display: flex;
  margin-left: 8rem;
  margin-top: 1rem;
}

.active {
  border-bottom: 1px solid #e0575b;
  font-size: 1.2rem;
}

.sub-nav li {
  height: 3rem;
  width: 6rem;
  line-height: 3rem;

  text-align: center;
  cursor: pointer;
}
.sub-nav li:hover {
  color: #e0575b;
}

.list {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.item {
  margin-top: 1rem;
  height: 3rem;
  width: 80%;

  box-shadow: 0 1px 1px gainsboro;

  display: flex;
  flex-direction: row;
  padding: 0.5rem;
}
.item .title {
  line-height: 2rem;
  padding-left: 1rem;
  font-size: 1rem;
  cursor: pointer;
  color: #3451b2;
}
.item .title:hover {
  color: #e0575b;
}

.item .image {
  width: 2rem;
  height: 2rem;
}
</style>
